//
//  SendWeiBoViewController.m
//  001LiycSinaWeiBo
//
//  Created by 李伟国 on 15/9/9.
//  Copyright (c) 2015年 李伟国. All rights reserved.
//

#import "SendWeiBoViewController.h"
#import "RDVTabBarController.h"
@interface SendWeiBoViewController ()<UIAlertViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UITextView *_textView;//用于输入微博内容
    UIImageView *_imageView;//展示图片
}
@end

@implementation SendWeiBoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"发送微博";
    
//    1.UITextView
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDHT, 100)];
    _textView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:_textView];
    
    
    
//    2.UIImageView
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 164 + 20, 160, 220)];
    _imageView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_imageView];
    
    
    
//    3.UIButton
    
    UIButton *selectPhotoBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    selectPhotoBnt.frame = CGRectMake(220, 220, 100, 100);
    selectPhotoBnt.backgroundColor = [UIColor orangeColor];
    [selectPhotoBnt addTarget:self action:@selector(selectPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:selectPhotoBnt];
    
    
//    4.右上角有一个发送微博的按钮
    UIBarButtonItem *sendBnt = [[UIBarButtonItem alloc] initWithTitle:@"send" style:UIBarButtonItemStylePlain target:self action:@selector(sendWeibo:)];
    self.navigationItem.rightBarButtonItem = sendBnt;
}

- (void)selectPhoto:(UIButton*)bnt {

    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"插入相片" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"相册",nil];
    [alertView show];
    alertView = nil;
}

#pragma mark --UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSLog(@"%ld",buttonIndex);
    
    if (buttonIndex == 1) {//拍照
        
        [self takePhoto];
        
    }else if (buttonIndex == 2) {//相册
    
        [self selectPhoto];
    
    }

}
//拍照
- (void)takePhoto {

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//        支持拍照功能
        //    1.创建UIImagePickerController 对象
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        //    2.设置枚举
        pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
        //    3.设置委托
        pickerVC.delegate = self;
        //    4.是否允许编辑
        pickerVC.allowsEditing = YES;
        [self presentViewController:pickerVC animated:YES completion:nil];
        
    }else {
    
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该设备不支持拍照功能" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        alertView = nil;
    
    }
    
    
    
    
}
//从相册里面选择照片
- (void)selectPhoto {

//    1.创建UIImagePickerController 对象
    UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
//    2.设置枚举
    pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    3.设置委托
    pickerVC.delegate = self;
//    4.是否允许编辑
    pickerVC.allowsEditing = YES;
    [self presentViewController:pickerVC animated:YES completion:nil];
    
}
#pragma mark --UIImagePickerControllerDelegate
//照片选择回调的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

    NSLog(@"%s",__func__);
    
    
    _imageView.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//取消照片选择
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    NSLog(@"%s",__func__);
    [picker dismissViewControllerAnimated:YES completion:nil];
}




#pragma mark --发送微博
- (void)sendWeibo:(id)sender {

    if (_imageView.image == nil) {//不带图片
        
        [DownLoadData sendWeiboWithBlock:^(id obj, NSError *error) {
            
            NSString *message = nil;
            if (obj) {
                message = @"发送成功";
            } else {
                message = @"发送失败";
            }
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alertView show];
            alertView = nil;
//            返回主页
            [self.navigationController popViewControllerAnimated:YES];
            
        } andContent:_textView.text];
        
    } else {//带图片
    
        [DownLoadData sendWeiboWithBlock:^(id obj, NSError *error) {
            
            NSString *message = nil;
            if (obj) {
                message = @"发送成功";
            } else {
                message = @"发送失败";
            }
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alertView show];
            alertView = nil;
            //            返回主页
            [self.navigationController popViewControllerAnimated:YES];
            
        } andContent:_textView.text andImage:_imageView.image];
    }
}

@end






